﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace Cad
{
    public class CAD
    {

        public CAD()
        {
            Sql = "";
            
            Cnx = @"Data Source =FLORIAN\SQLEXPRESS; Initial Catalog = HoloXia; Integrated Security = true; User = User; Password = User2015";
            SqlConnection connection = new SqlConnection(Cnx);
            Connection = connection;
            Command = null;
            Connection.Open();
        }


        private string Sql { get; set; }
        private string Cnx { get; set; }
        private SqlConnection Connection { get; set; }
        private SqlCommand Command { get; set; }
        private DataSet Dset { get; set; }
        private SqlDataAdapter Sda { get; set; }
        private SqlDataReader Sdr { get; set; }
        



        public void actionRows(string sql)
        {
            Sql = sql;
            Command = new SqlCommand(sql);
            Command.Connection = Connection;
            Command.ExecuteNonQuery();
        }

        public DataSet getRows(string sql, string dataTableName)
        {
            this.Sql = sql;
            Sda = new SqlDataAdapter(sql, Connection);
            Dset = new DataSet();
            Sda.Fill(Dset, dataTableName);
            return Dset;
        }

        public void close()
        {
            Connection.Close();
        }

        public void StockedProcedureExec(int param1, int param2, DateTime param3)
        {

            Dset = new DataSet();
            Sda = new SqlDataAdapter("DonneesMachineInsertDatas", Connection);
            Sda.SelectCommand.CommandType = CommandType.StoredProcedure;
            Sda.SelectCommand.Parameters.Add(new SqlParameter("@CPU", SqlDbType.Int));
            Sda.SelectCommand.Parameters["@CPU"].Value = param1;

            Sda.SelectCommand.Parameters.Add(new SqlParameter("@Memoire", SqlDbType.Int));
            Sda.SelectCommand.Parameters["@Memoire"].Value = param2;

            Sda.SelectCommand.Parameters.Add(new SqlParameter("@Date", SqlDbType.DateTime));
            Sda.SelectCommand.Parameters["@Date"].Value = param3;

            Sda.Fill(Dset, "DonneesMachineInsertDatas");

        }

        public void DonneeAppli(string param1, string param2, string param3)
        {
            Dset = new DataSet();
            Sda = new SqlDataAdapter("FileInsertDatas", Connection);
            Sda.SelectCommand.CommandType = CommandType.StoredProcedure;
            Sda.SelectCommand.Parameters.Add(new SqlParameter("@path", SqlDbType.VarChar));
            Sda.SelectCommand.Parameters["@path"].Value = param1;

            Sda.SelectCommand.Parameters.Add(new SqlParameter("@name", SqlDbType.VarChar));
            Sda.SelectCommand.Parameters["@name"].Value = param2;

            Sda.SelectCommand.Parameters.Add(new SqlParameter("@text", SqlDbType.Text));
            Sda.SelectCommand.Parameters["@text"].Value = param3;

            Sda.Fill(Dset, "FileInsertDatas");

        }

        public string SelectByName(string param1)
        {


            Command = new SqlCommand();
            Command.Connection = Connection;

            Command.CommandText = "SelectByName";
            Command.CommandType = CommandType.StoredProcedure;
            Command.Parameters.Add(new SqlParameter("@name", SqlDbType.VarChar));
            Command.Parameters["@name"].Value = param1;
            Sdr = Command.ExecuteReader();
            Sdr.Read();
            string result = Sdr.GetString(0);
            Sdr.Close();
            return result;
        }

    }
    

}
